package com.cn.gps;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.file.FileUtils;
import org.junit.Test;

import java.awt.geom.Point2D;
import java.text.DecimalFormat;

/**
 * Created by chenyuncong on 2019/9/5.
 */
public class GaodeToBaiduGPS {

    @Test
    public void test01() {
        String result = FileUtils.txt2String("D:\\data\\gps.txt");
        JSONObject obj = JSON.parseObject(result);
        JSONArray array = JSON.parseArray(obj.get("Data").toString());


        array.forEach(o->{
            JSONObject o9 = JSON.parseObject(o.toString());
//        o9.get("GPoints");
            JSONArray gPointsArray = JSON.parseArray(o9.get("GPoints").toString());
            JSONArray pointsArray = JSON.parseArray(o9.get("Points").toString());

//        System.out.println(gPointsArray.size());
//        System.out.println(pointsArray.size());

            StringBuffer sb = new StringBuffer("[");
            StringBuffer sb2 = new StringBuffer("[");
            for (int i = 0; i < 24; i++) {
                JSONObject gps = JSON.parseObject(gPointsArray.get(i).toString());
                double lng=Double.parseDouble(toStr6(gps.get("x").toString()));
                double lat =Double.parseDouble(toStr6(gps.get("y").toString()));
//                Point2D.Double p = GeoUtils.gpsToBaidu(lat,lng);
//                sb.append("{\"lng\":").append(p.getX()).append(",");
//                sb.append("\"lat\":").append(p.getY()).append("},");
                sb.append("{\"lng\":").append(lng).append(",");
                sb.append("\"lat\":").append(lat).append("},");

                JSONObject pGps = JSON.parseObject(pointsArray.get(i).toString());
                double lng2=Double.parseDouble(toStr6(pGps.get("X").toString()));
                double lat2 =Double.parseDouble(toStr6(pGps.get("Y").toString()));
                if(o9.get("FenceName").equals("沈海高速")){
                    lng2=lng2-0.003;
                    lat2=lat2+0.006;
                }
                Point2D.Double p2 = GeoUtils.gpsToBaidu(lat2,lng2);
                sb2.append("{\"lng\":").append(p2.getX()).append(",");
                sb2.append("\"lat\":").append(p2.getY()).append("},");
//                sb2.append("{\"lng\":").append(lng2).append(",");
//                sb2.append("\"lat\":").append(lat2).append("},");
            }
            if (sb.length() > 1) {
                sb.deleteCharAt(sb.length() - 1);
            }
            if (sb2.length() > 1) {
                sb2.deleteCharAt(sb2.length() - 1);
            }
            sb.append("]");
            sb2.append("]");
            System.out.println(sb.toString());
            System.out.println(sb2.toString());
            System.out.println(o9.get("Remark"));
            System.out.println(o9.get("FenceName"));
            System.out.println(o9.get("AlarmSpeed"));
            System.out.println();

        });



    }

    DecimalFormat df = new DecimalFormat("#.000000");
    private String toStr6(String str) {
        Double d = Double.parseDouble(str);
        return df.format(d);
    }
}
